import router from '@/router'
import { variablesRoutes, constantRoutes } from '@/router' // 用于路由筛选
const state = {
  routes: [] // 记录所有路由
}
const mutations = {
  setRouter(state, payload) {
    state.routes = payload
  }
}
const actions = {
  filterRouter(context, roles) {
    const routes = variablesRoutes.filter((item) => { return roles.menus.includes(item.name) })
    // 动态追加路由 404要放在最后
    router.addRoutes([...routes, { path: '*', redirect: '/404', hidden: true }])
    // 必须要加，是路由已知的bug

    // routes 筛选之后的动态路由+静态路由
    context.commit('setRouter', [...constantRoutes, ...routes])
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions

}
